package com.ling.tts3;

import android.content.Intent;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;

import com.ling.tts3.utils.TTSUtil;

import java.util.Locale;

public class TTS2Activity extends AppCompatActivity {

    private static final int MY_DATA_CHECK_CODE = 100;
    private static String TAG = "TTS2Activity";
    // tts
    private TTSUtil tts;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_tts1);

        Button button = findViewById(R.id.test);
        TextView textView = findViewById(R.id.edit);

        tts = new TTSUtil(this, new TTSUtil.TTSListener() {
            @Override
            public void onInitSuccess() {
                Log.d(TAG,"TTS TTSUtil onInitSuccess: 初始化成功");
            }
            @Override
            public void onInitFailure() {
                Log.d(TAG,"TTS TTSUtil onInitFailure: 初始化失败");
            }
            @Override
            public void onSpeechStart() {
                Log.d(TAG,"TTS TTSUtil onSpeechStart: 开始");
            }
            @Override
            public void onSpeechDone() {
                Log.d(TAG,"TTS TTSUtil onSpeechDone: 完成");
            }
            @Override
            public void onSpeechError(String errorMessage) {
                Log.d(TAG,"TTS TTSUtil onSpeechError: 错误:"+errorMessage);
            }

        });

        button.setOnClickListener(v -> {
            String text = textView.getText().toString();
            Log.d(TAG,"转换:"+text);
            if (!text.isEmpty()) {
//                Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
                tts.speak(text);
            }
        });

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (tts != null) {
            tts.release();
        }
    }

}